// TODO What is this context id stuff in globals xml
// What it saves the block context for custom blocks in the xml

function parseXml(xml) {
    var domParser = new DOMParser();
    return domParser.parseFromString(xml, "text/xml");
}

var GeneratorFunction = Object.getPrototypeOf(function*(){}).constructor;

/*
import("/turbosnap/script-compiler.js")
.then((module) => {
    var xml = parseXml(TEST);
    var defXml = parseXml(DEF_TEST);

    var globalScope = new module.Scope();
    globalScope.prefix = "env.globals.";
    globalScope.addVariable("a");

    var defSrc = module.compileDefinition(defXml.firstChild, globalScope);
    console.log(defSrc);

    var src = module.compileScript(xml.firstChild, globalScope);
    console.log(src);

    //var genFunc = new GeneratorFunction("env", src);
    //var task = genFunc(script_env);

    //runTask(task);
});
*/

import("/turbosnap/project.js").then((module) => {
    fetch("examples/sierpinski.xml").then(res => res.text())
        .then(data => {
            var canvas = document.getElementById("canvas");

            var xml = parseXml(data).firstChild;
            var project = new module.Project(canvas);
            project.loadXml(xml);
            console.log(project);

            window.addEventListener("keydown", (ev) => project.keyDown(ev));
            window.addEventListener("keyup", (ev) => project.keyUp(ev));
            canvas.addEventListener("mousedown", (ev) => project.mouseDown(ev));
            canvas.addEventListener("mouseup", (ev) => project.mouseUp(ev));
            window.addEventListener("mousemove", (ev) => project.mouseMove(ev));

            window.runProject = function() {
                project.go();
            };

            window.stopProject = function() {
                project.stop();
            };

            window.loadProject = function() {
                var input = prompt("enter [username]/[project name]");

                var [username, projectName] = input.split("/");

                if (projectName) {
                    fetch(`https://snap.berkeley.edu/api/v1/projects/${username}/${projectName}`)
                        .then(res => res.text())
                        .then(data => {
                            var xml = parseXml(data).firstChild.firstChild;
                            project.reset();
                            project.loadXml(xml);
                        });
                } else {
                    fetch(`examples/${username}.xml`)
                        .then(res => res.text())
                        .then(data => {
                            var xml = parseXml(data).firstChild;
                            project.reset();
                            project.loadXml(xml);
                        })
                }
            }

            var last = performance.now();

            // TODO: opengl drawing
            (function update(now) {
                requestAnimationFrame(update);
                var dt = now - last;
                last = now;
                project.tick(dt);
                project.render(canvas);
            })(last);
        })
});